package com.itheima.springbootwebtest.controller;

import com.itheima.springbootwebtest.pojo.EmpLoginDTO;
import com.itheima.springbootwebtest.pojo.LoginVO;
import com.itheima.springbootwebtest.pojo.Result;
import com.itheima.springbootwebtest.properties.JWTProperties;
import com.itheima.springbootwebtest.service.EmpService;
import com.itheima.springbootwebtest.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制层
 *
 * @Author mr.luo
 * @Date 14 4月 2025 11:21
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;
    @Autowired
    private JWTProperties jwtProperties;

    /**
     * 登录
     *
     * @return
     */
    @GetMapping("/login")
    public Result login(@RequestBody EmpLoginDTO empLoginDTO) {
        LoginVO loginVO = empService.login(empLoginDTO);
        //生成JWT
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", loginVO.getId());
        String token = JWTUtils.CreateJWT(jwtProperties.getSecretKey(), jwtProperties.getExpire(), claims);
        loginVO.setToken(token);
        log.info("生成令牌成功, {}", loginVO);
        return Result.success(loginVO);
    }
}
